<template>
  <div>
    <el-row>
      <el-col :lg="18" :md="12">
      <el-tabs v-model="activefname" @tab-click="handleClick">
        <el-tab-pane v-for="item in weekList" :label="item.numtxt" :key="item.numtxt" :data-date="item.datearr" :name="item.tabname"/>
      </el-tabs>
      </el-col>
      <el-col :lg="6" :md="12">
        <el-button
          @click="openImport(row)"
          type="primary"
          class="ele-btn-icon"
          size="small">导入
        </el-button>
        <el-button
          @click="curriculumExport()"
          type="primary"
          class="ele-btn-icon"
          size="small">导出
        </el-button>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-copy-document"
          class="ele-btn-icon"
          @click="openFzxq()"
        >复制学期
        </el-button>
      </el-col>
    </el-row>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :need-page="false"
      :datasource="url"
      :columns="columns"
      :parseData="parseData"
      :where="where"
      @cell-click="handleCellClick"
      height="calc(100vh - 261px)"
      class="custom-table"
      tool-class="ele-toolbar-form">
      <!-- 表头工具栏 -->
      <template slot="toolbar">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-copy-document"
          class="ele-btn-icon"
          @click="copyData()"
        >复制本周
        </el-button>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-copy-document"
          class="ele-btn-icon"
          @click="copyDataxq()"
        >复制本周到学期
        </el-button>
      </template>
      <!-- 列 -->
      <template slot="zhou1" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id1']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date1']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx1'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx1'][1]}}</span>
        <span v-if="row['zhou1']">{{row['zhou1'][0]}}<br/>{{row['zhou1'][1]}}</span>
      </template>

      <template slot="zhou2" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id2']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date2']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx2'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx2'][1]}}</span>
        <span v-if="row['zhou2']">{{row['zhou2'][0]}}<br/>{{row['zhou2'][1]}}</span>
      </template>
      <template slot="zhou3" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id3']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date3']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx3'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx3'][1]}}</span>
        <span v-if="row['zhou3']">{{row['zhou3'][0]}}<br/>{{row['zhou3'][1]}}</span>
      </template>
      <template slot="zhou4" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id4']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date4']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx4'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx4'][1]}}</span>
        <span v-if="row['zhou4']">{{row['zhou4'][0]}}<br/>{{row['zhou4'][1]}}</span>
      </template>
      <template slot="zhou5" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id5']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date5']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx5'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx5'][1]}}</span>
        <span v-if="row['zhou5']">{{row['zhou5'][0]}}<br/>{{row['zhou5'][1]}}</span>
      </template>
      <template slot="zhou6" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id6']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date6']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx6'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx6'][1]}}</span>
        <span v-if="row['zhou6']">{{row['zhou6'][0]}}<br/>{{row['zhou6'][1]}}</span>
      </template>
      <template slot="zhou7" slot-scope="{row}">
        <span class="cid" style="display: none;">{{row['id7']}}</span>
        <span class="wdate" style="display: none;">{{labelweek['date7']}}</span>
        <span class="dtxtc" style="display: none;">{{row['dtx7'][0]}}</span>
        <span class="dtxtu" style="display: none;">{{row['dtx7'][1]}}</span>
        <span v-if="row['zhou7']">{{row['zhou7'][0]}}<br/>{{row['zhou7'][1]}}</span>
      </template>


      <!-- 状态列 -->
      <template slot="status" slot-scope="{row}">
        <el-switch
          v-model="row.status"
          @change="editStatus(row)"
          :active-value="1"
          :inactive-value="2"/>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <curriculum-data-edit
      :visible.sync="showEdit"
      :data="current"
      @done="reload"/>
    <curriculum-data-fzxq
      :visible.sync="showfzxq"
      :data="current"
      :stype="stype"
      :cId="configId"
      @done="reload"/>
    <curriculum-import
      :data="current"
      :visible.sync="showcurriculumImport"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import CurriculumDataEdit from './curriculum-data-edit';
import CurriculumDataFzxq from './curriculum-data-fzxq';
import CurriculumImport from './curriculum-import';
import {download} from "@/utils/download";

export default {
  name: 'ConfigData',
  components: {CurriculumDataEdit,CurriculumDataFzxq,CurriculumImport},
  computed: {
    ...mapGetters(["permission"]),
  },
  props: {
    // 配置id
    configId: Number,
    stype:Number,
  },
  data() {
    return {
      // 表格数据接口
      url: '/curriculum/index',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'jie',
          label: '节次',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'time',
          label: '时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
        },
        {
          prop: 'zhou1',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou1'],
                h('el-tooltip', { attrs: { content: this.labelweek['date1'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          customRender: (row, column, cellValue) => {
            return <span>{cellValue[0]}-{cellValue[1]}</span>;
          },
          align: 'center',
          minWidth: 150,
          slot: 'zhou1'
        },
        {
          prop: 'zhou2',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou2'],
                h('el-tooltip', { attrs: { content: this.labelweek['date2'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'zhou2'
        },
        {
          prop: 'zhou3',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou3'],
                h('el-tooltip', { attrs: { content: this.labelweek['date3'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'zhou3'
        },
        {
          prop: 'zhou4',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou4'],
                h('el-tooltip', { attrs: { content: this.labelweek['date4'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'zhou4'
        },
        {
          prop: 'zhou5',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou5'],
                h('el-tooltip', { attrs: { content: this.labelweek['date5'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'zhou5'
        },
        {
          prop: 'zhou6',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou6'],
                h('el-tooltip', { attrs: { content: this.labelweek['date6'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'zhou6'
        },
        {
          prop: 'zhou7',
          renderHeader:(h)=>{
            return h('span', [
                this.labelweek['zhou7'],
                h('el-tooltip', { attrs: { content: this.labelweek['date7'], placement: 'top' } }, [
                  h('i', { class: 'el-icon-question' })
                ])
              ]);
          },
          className:'hovbs',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'zhou7'
        },
      ],
      activefname: 'first1',
      activefweek: [],
      labelweek: [],
      sid: 0,
      wid:0,
      // 表格搜索条件
      where: {
        type:this.stype,
        cId: this.configId,
        start_end: this.activefweek
      },
      // 当前编辑数据
      current: null,
      coursedate:'',
      coursesele:'',
      // 是否显示编辑弹窗
      showEdit: false,
      showfzxq: false,
      showcurriculumImport:false,
      weekList:[]
    };
  },
  mounted() {
    this.getWeekList();
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    parseData(val) {
          // console.log(val,'返回val=====');
          this.labelweek = val.labelweek;
          this.sid = val.sid;
          this.wid = val.wid;
          return val
        },
    handleCellClick(row, column,  cell) {
        this.current = {
          type:this.stype,
          tid:this.configId,
          semester_id:this.sid,
          work_id:this.wid,
          course_section:row.jienum,
          course_date:cell.querySelector('.wdate').innerText
        };
        if(cell.querySelector('.cid').innerText!=''){
          this.current.id = parseInt(cell.querySelector('.cid').innerText);
        }
        if(cell.querySelector('.dtxtc').innerText!=''){
          this.current.course_id = parseInt(cell.querySelector('.dtxtc').innerText);
        }
        if(cell.querySelector('.dtxtu').innerText!=''){
          this.current.teacher_id = parseInt(cell.querySelector('.dtxtu').innerText);
        }
        this.showEdit = true;

      },
    handleClick(tab) {
      this.where.start_end = tab.$attrs['data-date'];
      this.activefweek = tab.$attrs['data-date'];
      // console.log(this.where);
      this.reload();
        // console.log(tab, event);
      },
      /* 复制 */
      copyData(){
        this.$confirm('确定要复制这一周数据到下一周吗?', '提示', {
          type: 'warning'
        }).then(() => {
            const loading = this.$loading({lock: true});
            this.$http.post('/curriculum/copyoneweek',
            {
              type:this.stype,
              cId: this.configId,
              start_end: this.activefweek
            })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                // this.$emit('done');
                // this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            }).catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        }).catch(() => {
        });
      },

      /* 复制 */
      copyDataxq(){
        this.$confirm('确定要复制这一周数据到本学期吗?', '提示', {
          type: 'warning'
        }).then(() => {
            const loading = this.$loading({lock: true});
            this.$http.post('/curriculum/copyoneweekxq',
            {
              type:this.stype,
              cId: this.configId,
              start_end: this.activefweek
            })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                // this.$emit('done');
                // this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            }).catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        }).catch(() => {
        });
      },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    openFzxq() {
      this.showfzxq = true;
    },

    openImport(row) {
      this.current = row;
      this.showcurriculumImport = true;
    },

    /* 导出数据Excel */
    curriculumExport() {
      download('/curriculum/exportExcel', this.where);
      // let info = JSON.parse(JSON.stringify(this.where));
      // this.$http
      //   .post("/curriculum/exportExcel", info)
      //   .then((res) => {
      //     if (res.data.code == 0) {
      //       // 下载文件
      //       window.location.href = res.data.data;
      //       this.$message({
      //         type: "success",
      //         message: "导出成功",
      //       });
      //     } else {
      //       this.$message.error("导出失败");
      //     }
      //   })
    },

    /**
     * 获取周期列表
     */
    getWeekList() {
      this.$http.post('/curriculum/getWeekList',{type:this.stype,cid:this.configId}).then(res => {
        if (res.data.code === 0) {
          this.weekList = res.data.data;
          this.activefname = res.data.first;
          this.activefweek = res.data.datearr;
          this.where.start_end = this.activefweek;
          this.reload();
        } else {
          this.where.start_end = [];
          this.reload();
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  },
  watch: {

    // 监听配置id变化
    configId() {
      this.getWeekList();
      this.where.cId = this.configId;
      // this.where.start_end = this.activefweek;

      // if(this.where.cId&&this.where.start_end){
      //   this.reload();
      // }

    },
    start_end(){
      this.getWeekList();
      if(this.where.cId&&this.where.start_end){
        this.reload();
      }
    }
  }
}
</script>

<style scoped>
  .custom-table ::v-deep tr{
    height: 100px;;
  }
  .custom-table ::v-deep .el-table__row > td.hovbs {
    cursor: pointer;
  }
  .custom-table ::v-deep td.hovbs:hover {
        background-color: #55aaff !important;
        color: #fff;
      }
</style>
